//
//  SNTool.m
//  app
//
//  Created by 王少宁 on 15/7/15.
//  Copyright (c) 2015年 hdaren. All rights reserved.
//

#import "SNTool.h"
#import "UIImage+Conveniently.h"

@implementation SNTool

+ (NSString *)timesFromNowToDateline:(NSString *)dateline
{
    //时间戳转换成的date
    NSDate * datelineDate = [NSDate dateWithTimeIntervalSince1970:[dateline floatValue]];
    
    NSTimeInterval aTimer = [datelineDate timeIntervalSinceDate:[NSDate date]];
    
    float remnantDayFloat = (float)aTimer / (3600*24);
    //剩余多少天
    int remnantDays = (int)(remnantDayFloat /1) == remnantDayFloat ? (int)(remnantDayFloat /1) : (int)(remnantDayFloat /1)+1;
    //剩余多少小时
    int remnantHours = (float)(aTimer/3600) == (int)(aTimer/3600) ? (int)(aTimer/3600) : (int)(aTimer/3600) + 1;
    //剩余多少分
    int remnantMinutes = (float)(aTimer / 60) == (int)(aTimer / 60) ? (int)(aTimer / 60) : (int)(aTimer / 60) + 1;
    
    if (remnantDayFloat >= 6) {//大于 6 天不显示
        return [NSString stringWithFormat:@""];
    } else if (remnantDayFloat >=1 && remnantDayFloat < 6) {
        return [NSString stringWithFormat:@"剩%d天",remnantDays];
    } else if (remnantDayFloat <1 && aTimer >= 3600) {
        return [NSString stringWithFormat:@"剩%d小时",remnantHours];
    } else if (0 < aTimer && aTimer < 3600) {
        return [NSString stringWithFormat:@"剩%d分",remnantMinutes];
    } else {
        return @"已结束";
    }
}



+ (NSString *)timeWithStartTimel:(NSTimeInterval)startTimel endTimel:(NSTimeInterval)endTimel
{
    NSTimeInterval nowTimeInterval = [[NSDate date] timeIntervalSince1970];
    
    if (nowTimeInterval < startTimel) {
        //时间戳转换成的date
        NSDate * startDate = [NSDate dateWithTimeIntervalSince1970:startTimel];
        
        NSDateFormatter * formatter1 = [[NSDateFormatter alloc] init];
        formatter1.dateFormat = @"yyyy-MM-dd";
        
        NSString * startDateStr = [formatter1 stringFromDate:startDate];
        NSString * nowDateStr = [formatter1 stringFromDate:[NSDate date]];
        
        NSDate * startDateByMMDD = [formatter1 dateFromString:startDateStr];
        NSDate * nowDateByMMDD = [formatter1 dateFromString:nowDateStr];
        
        NSTimeInterval aTimer = [startDateByMMDD timeIntervalSinceDate:nowDateByMMDD];
        
        NSDateFormatter * formatter2 = [[NSDateFormatter alloc] init];
        formatter2.dateFormat = @"hh:mm";
        
        NSString * startDateStr2 = [formatter2 stringFromDate:startDate];
        
        NSDateFormatter * formatter3 = [[NSDateFormatter alloc] init];
        formatter3.dateFormat = @"MM/dd hh:mm";
        
        NSString * startDateStr3 = [formatter3 stringFromDate:startDate];
        
        float days = aTimer / (3600*24);
        
        if (days == 0) {
            
            NSDate * datelineDt = [formatter2 dateFromString:startDateStr2];
            NSString * nowStr = [formatter2 stringFromDate:[NSDate date]];
            NSDate * nowDt = [formatter2 dateFromString:nowStr];
            
            NSTimeInterval todayATimer = [datelineDt timeIntervalSinceDate:nowDt];
            
            if (todayATimer < 0) {
                return kFinishSellStr;
            } else {
                return [NSString stringWithFormat:@"今天%@开售",startDateStr2];
            }
            
        } else if (days == 1) {
            return [NSString stringWithFormat:@"明天%@开售",startDateStr2];
        } else {
            return [NSString stringWithFormat:@"%@开售",startDateStr3];
        }
    } else if (nowTimeInterval > endTimel) {
        return kFinishSellStr;
    } else {//其实这种情况并不存在
        return kFinishSellStr;
    }
    
}


+ (NSDictionary *)countDownWithTimeInterval:(NSTimeInterval)aTimer
{
    int hours = (int)aTimer / 3600;
    int minutes = ((int)aTimer%3600) / 60;
    int seconds = ((int)aTimer%3600) % 60;
    
    NSString * hoursStr;
    if (hours / 10 == 0) {
        hoursStr = [NSString stringWithFormat:@"%.2d",hours];
    } else {
        hoursStr = [NSString stringWithFormat:@"%d",hours];
    }
    
    NSDictionary * timerDict = @{@"atimer":[NSString stringWithFormat:@"%f",aTimer],
                                 @"hours":hoursStr,
                                 @"minutes":[NSString stringWithFormat:@"%.2d",minutes],
                                 @"seconds":[NSString stringWithFormat:@"%.2d",seconds]};
    
    return timerDict;
}


#warning - 这个方法现在已经不用了
+ (BOOL)temaiWithStartTime:(NSTimeInterval)startTime endTime:(NSTimeInterval)endTime
{
    NSDate * nowDate = [NSDate date];
    NSString * time1970 = @"1970/1/1 08:00:00";
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    formatter.dateFormat = @"yyyy/MM/dd HH:mm:ss";
    NSDate * date1970 = [formatter dateFromString:time1970];
    
    NSTimeInterval aTimer = [nowDate timeIntervalSinceDate:date1970];
    
    BOOL temai;
    if (aTimer > startTime && aTimer < endTime) {
        temai = YES;
    } else {
        temai = NO;
    }
    
    return temai;
}

+ (NSData *)imageMaxWidthOrHeight720DataWithImage:(UIImage *)image
{
    NSData * imgData;
    if (image.size.width > 720 || image.size.height > 720) {
        
        if (image.size.width > image.size.height) {
            imgData = [UIImage dataOriginImage:image scaleToSize:CGSizeMake(720, 720 * image.size.height/image.size.width) fileToRatio:0.8];
        } else {
            imgData = [UIImage dataOriginImage:image scaleToSize:CGSizeMake(720 * image.size.width/image.size.height, 720) fileToRatio:0.8];
        }
        
    } else {
        imgData = [UIImage dataOriginImage:image scaleToSize:CGSizeMake(image.size.width, image.size.height) fileToRatio:0.8];
    }
    return imgData;
}

+ (NSString *)removeBlankWithStr:(NSString *)str
{
    str = [str stringByReplacingOccurrencesOfString:@"\\s" withString:@""
                                            options:NSRegularExpressionSearch range:NSMakeRange(0, str.length)];
    return str;
}



+ (NSString *)typeForImageData:(NSData *)data
{
    
    
    uint8_t c;
    
    [data getBytes:&c length:1];
    
    
    
    switch (c) {
            
        case 0xFF:
            
            return @"image/jpeg";
            
        case 0x89:
            
            return @"image/png";
            
        case 0x47:
            
            return @"image/gif";
            
        case 0x49:
            
        case 0x4D:
            
            return @"image/tiff";
            
    }
    
    return nil;
    
}


+ (BOOL)shouldLoginFromController:(UIViewController *)controller
{
    if ([[NSUserDefaults standardUserDefaults] objectForKey:@"uid"]) {
        return NO;
    }
    
    [[SNBaseInfoManager shareManager] setBaseInfoObject:NSStringFromClass(controller.class) forKey:kLastVCToLoginVC];
    
    HDAppDelegate* app = (HDAppDelegate*)[[UIApplication sharedApplication]delegate];
    app.storyBoard = [UIStoryboard storyboardWithName:@"LogInOutStoryboard" bundle:nil];
    UIViewController * loginVC = app.storyBoard.instantiateInitialViewController;
    [controller.navigationController presentViewController:loginVC animated:YES completion:nil];
    
    return YES;
}



@end
